let fs = require("fs");
let path = require("path");

//首先创建100个文件
let createFile = {
    createPath: function (dir) {
        if (fs.existsSync(dir)) {
            return;
        }
        fs.mkdirSync(dir);
    },
    createContent: function (num) {
        return num+"";
    },

    createFile: function (filePath, content) {
        console.log(filePath);
        let fd = fs.openSync(filePath, 'w');
        fs.writeSync(fd, content);
        fs.closeSync(fd);
    },

    handle: function (num, dir) {
        this.createPath(dir);
        for (let i = 1; i < num; i++) {
            if (i < 10) {
                let fileName = dir + '/' + +'0' + i + '.txt';
                let content = this.createContent(i);
                this.createFile(fileName, content);
            } else {
                let fileName = dir + '/' + i + '.txt';
                let content = this.createContent(i);
                this.createFile(fileName, content); 
            }

        }
    }
}

createFile.handle(101, './demo01');

//读文件
fs.readFile('./demo01/01.txt', { encoding: 'utf8', flag: 'r' }, function (err, string) {
    if (err) {
        console.log(err)
    } else {
        console.log(string)
    }
});